<?php
namespace app\service;
use think\facade\Db;
class roomService{
    //判断小区是否重复
    public function roominfoName($community_id,$group,$building,$unit,$room){
        return Db::name('room')
                ->where('community_id',$community_id)
                ->where('group',$group)
                ->where('building',$building)
                ->where('unit',$unit)
                ->where('room',$room)
                ->find();
    }
    //根据当前账号查询小区信息
    public function communityList($com){
        return Db::name('community')->order('id asc')->where($com)->select();
    }
    //根据小区ID查询小区名称
    public function communityIdName($id){
        return Db::name('community')->where('id',$id)->find();
    }
    //判断房屋是否重复
    public function roomName($building,$unit,$room){
        return Db::name('room')->where('building',$building)->where('unit',$unit)->where('room',$room)->find();
    }
    //房屋添加
    public function roomAdd($data){
        return Db::name('room')->insert($data);
    }
    //根据房屋ID查询房屋信息
    public function roomId($id){
        return Db::name('room')->where('id',$id)->find();
    }
    //房屋修改
    public function roomEdit($data){
        return Db::name('room')->update($data);
    }
    //房屋删除
    public function roomDel($id){
        return Db::name('room')->where('id', $id)->delete();
    }
    //查询指定id是否存在
    public function checkRoomInfo($data){
        if (is_array($data)) {
            return Db::name('room')->where('id', 'in', $data)->select();
        }else{
            return Db::name('room')->where('id', $data)->find();
        }
    }
    //-----------------------------------------多条件查询房屋信息-----------------------------------------
    public function disgroup($community_id){
        return Db::table("room")->where("community_id",$community_id)->distinct("group")->column("group");
    }
    public function disbuilding($community_id,$group){
        return Db::table("room")->where("community_id",$community_id)->where("group",$group)->distinct("building")->column("building");
    }
    public function disunit($community_id,$group,$building){
        return Db::table("room")->where("community_id",$community_id)->where("group",$group)->where("building",$building)->distinct("unit")->column("unit");
    }
    public function disroom($community_id,$group,$building,$unit){
        return Db::table("room")->where("community_id",$community_id)->where("group",$group)->where("building",$building)->where("unit",$unit)->distinct("room")->column("room");
    }
    //根据小区名称查找楼栋信息
    public function whebuildingList($community){
        return Db::name('room')->where('community',$community)->field('building')->order('id asc')->select();
    }
    //根据小区楼栋查询单元信息
    public function wheunitList($community,$building){
        return Db::name('room')->where('community',$community)->where('building',$building)->field('unit')->order('id asc')->select();
    }
    //根据小区楼栋和单元信息查找房间号
    public function wheroomList($community,$building,$unit){
        return Db::name('room')->where('community',$community)->where('building',$building)->where('unit',$unit)->field('room')->order('id asc')->select();
    }
    //查询条数
    public function roomWhereNumber($where){
        return Db::name('room')->where($where)->count();
    }
    //多条件查询
    public function roomWhereList($page,$where){
        return Db::name('room')->where($where)->order('id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    //根据小区编号查询小区名称
    public function communityName($comid){
        return Db::name('community')->where('id' , $comid)->find();
    }
    //通过小区名称查询对应的小区id
    public function communityNameId($communityName){
        return Db::name('community')->where('name', $communityName)->find();
    }
    public function roomExcelAdd($data){
        return Db::name('room')->insertGetId($data);
    }
    //----------------------------------------------验证----------------------------------------------
    //根据门锁序列号和支付宝ID判断是否已认证
    public function userIdInfo($userid,$serial){
        $del = Db::name('personnel')->where('out_time','<',date('Y-m-d H:i:s',time()))->delete();
        return Db::name('personnel')->where('alipay',$userid)->where('serial','like','%'.$serial.'%')->where('out_time','>=',date('Y-m-d H:i:s',time()))->where('status',1)->find();
    }
    //根据门锁序列号查询小区ID
    public function serialCommunity($serial){
        return Db::name('lock')->where('serial',$serial)->value('community');
    }
    //根据小区ID查询小区信息
    public function communityIdInfo($id){
        return Db::name('community')->where('id',$id)->find();
    }
    //查询门锁类型
    public function lockType($serial){
        return Db::name('lock')->where('serial',$serial)->value('locktype');
    }
    //根据房屋ID查询房间信息
    public function roomIdInfo($id){
        return Db::name('room')->where('id',$id)->find();
    }
    //添加开门记录
    public function recordAdd($record){
        return Db::name('record')->insert($record);
    }
    //根据门锁序列号和支付宝ID判断是否以提交认证信息
    public function userIdStatusInfo($userid,$serial){
        return Db::name('personnel')->where('alipay',$userid)->where('serial','like','%'.$serial.'%')->where('out_time','>=',date('Y-m-d H:i:s',time()))->where('status',2)->find();
    }
    //根据门锁查询小区信息
    public function communitySerialInfo($serial){
        $id = Db::name('lock')->where('serial',$serial)->value('community');
        return Db::name('community')->where('id',$id)->find();
    }
    //根据提交信息查询房屋信息
    public function roomInfo($community,$group,$building,$unit,$room){
        return Db::name('room')->where('community',$community)->where('group',$group)->where('building',$building)->where('unit',$unit)->where('room',$room)->find();
    }
    //根据手机号查询验证码
    public function provingInfo($phone){
        return Db::name('proving')->where('phone',$phone)->order('id desc')->value('number');
    }
    //判断是否已存在用户
    public function personnelInfo($community,$room_id,$relation){
        return Db::name('personnel')->where('community',$community)->where('room_id',$room_id)->where('relation',$relation)->where('status',1)->find();
    }
    //提交用户信息
    public function personnelAdd($personnel){
        return Db::name('personnel')->insert($personnel);
    }
    //查询事由信息
    public function causelist(){
        return Db::name('cause')->select();
    }
    //访客记录添加
    public function visitorAdd($data){
        return Db::name('visitor')->insert($data);
    }
    //查询楼栋锁信息
    public function unitLock($id){
        $room = Db::name('room')->where('id',$id)->find();
        return Db::name('lock')->where('position',$room['group'].$room['building'].'幢'.$room['unit'].'单元')->value('serial');
    }
    //查询大门锁
    public function communityLock($community){
        return Db::name('lock')->where('community',$community)->where('locktype', 1 )->column('serial');
    }
    //查询别墅锁
    public function villaLock($community){
        return Db::name('lock')->where('community',$community)->where('locktype', 3 )->column('serial');
    }
    //验证码添加
    public function provingAdd($data){
        $del = Db::name('proving')->where('time','<',date('Y-m-d H:i:s',strtotime("-1 day")))->delete();
        return Db::name('proving')->insert($data);
    }
    //判断当前账号是否已存在
    public function userIdPersonnelInfo($userid,$room_id){
        return Db::name('personnel')->where('alipay',$userid)->where('room_id',$room_id)->find();
    }
    //在已存在账号上添加当前锁
    public function personnelEdit($personnel){
        return Db::name('personnel')->update($personnel);
    }
    //根据业主支付宝查询房号
    public function roomIdList($userid){
        return Db::name('personnel')->where('alipay',$userid)->where('relation',1)->where('status',1)->column('room_id');
    }
    public function personnelAllowList($roomid){
        return Db::name('personnel')->where('room_id',$roomid)->where('relation','>',1)->where('status',2)->select();
    }
    //查询人员请求是否存在
    public function personnelIdinfo($id){
        return Db::name('personnel')->where('id',$id)->find();
    }
    //允许
    public function personnelIdAllow($id){
        return Db::name('personnel')->where('id',$id)->update(['status' => 1]);
    }
    //删除
    public function personnelIdDel($id){
        return Db::name('personnel')->delete($id);
    }
    //查询条数
    public function personnelListNumber($where,$community_id){
        return Db::name('personnel')->where("community_id","in",$community_id)->where($where)->count();
    }
    public function personnelList($page,$where,$community_id){
        return Db::name('personnel')->where("community_id","in",$community_id)->where($where)->order('id desc')->paginate(15, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }

    public function personcheckListNumber($where,$community_id){
        return Db::name('person_check')->where("community_id","in",$community_id)->where($where)->count();
    }
    public function personcheckList($page,$where,$community_id){
        return Db::name('person_check')->where("community_id","in",$community_id)->where($where)->order('id desc')->paginate(50, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    //查询房号
    public function whereRoom($whereroom){
        return Db::name('room')->where($whereroom)->column('id');
    }
}